package com.hjxr.quota.datasource.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.hjxr.quota.datasource.domain.JobDatasource;
import com.hjxr.quota.datasource.service.DatasourceQueryService;
import com.hjxr.quota.datasource.service.JobDatasourceService;
import com.hjxr.quota.tool.database.ColumnInfo;
import com.hjxr.quota.tool.query.BaseQueryTool;
//import com.hjxr.quota.tool.query.HBaseQueryTool;
import com.hjxr.quota.tool.query.MongoDBQueryTool;
import com.hjxr.quota.tool.query.QueryToolFactory;
import com.hjxr.quota.util.JdbcConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * datasource query
 *
 * @author zhouhongfa@gz-yibo.com
 * @ClassName JdbcDatasourceQueryServiceImpl
 * @Version 1.0
 * @since 2019/7/31 20:51
 */
@Service
public class DatasourceQueryServiceImpl implements DatasourceQueryService {

    @Autowired
    private JobDatasourceService jobDatasourceService;

    @Override
    public List<String> getDBs(Integer id) throws IOException {
        //获取数据源对象
        JobDatasource datasource = jobDatasourceService.getById(id);
        return new MongoDBQueryTool(datasource).getDBNames();
    }


    @Override
    public List<String> getTables(Integer id) throws IOException {
        //获取数据源对象
        JobDatasource datasource = jobDatasourceService.getById(id);
        //queryTool组装
        if (ObjectUtil.isNull(datasource)) {
            return Lists.newArrayList();
        }
        /*if (JdbcConstants.HBASE.equals(datasource.getDatasource())) {
            return new HBaseQueryTool(datasource).getTableNames();
        } else*/ if (JdbcConstants.MONGODB.equals(datasource.getDatasource())) {
            return new MongoDBQueryTool(datasource).getCollectionNames(datasource.getDatabaseName());
        } else {
            BaseQueryTool qTool = QueryToolFactory.getByDbType(datasource);
            return qTool.getTableNames();
        }
    }

    @Override
    public List<String> getCollectionNames(Integer id, String dbName) throws IOException {
        //获取数据源对象
        JobDatasource datasource = jobDatasourceService.getById(id);
        //queryTool组装
        if (ObjectUtil.isNull(datasource)) {
            return Lists.newArrayList();
        }
        return new MongoDBQueryTool(datasource).getCollectionNames(dbName);
    }


    @Override
    public List<String> getColumns(Integer id, String tableName) throws IOException {
        //获取数据源对象
        JobDatasource datasource = jobDatasourceService.getById(id);
        //queryTool组装
        if (ObjectUtil.isNull(datasource)) {
            return Lists.newArrayList();
        }
        /*if (JdbcConstants.HBASE.equals(datasource.getDatasource())) {
            return new HBaseQueryTool(datasource).getColumns(tableName);
        } else */if (JdbcConstants.MONGODB.equals(datasource.getDatasource())) {
            return new MongoDBQueryTool(datasource).getColumns(tableName);
        } else {
            BaseQueryTool queryTool = QueryToolFactory.getByDbType(datasource);
            return queryTool.getColumnNames(tableName, datasource.getDatasource());
        }
    }

    @Override
    public List<String> getColumnsByQuerySql(Integer datasourceId, String querySql) {
        //获取数据源对象
        JobDatasource jdbcDatasource = jobDatasourceService.getById(datasourceId);
        //queryTool组装
        if (ObjectUtil.isNull(jdbcDatasource)) {
            return Lists.newArrayList();
        }
        BaseQueryTool queryTool = QueryToolFactory.getByDbType(jdbcDatasource);
        return queryTool.getColumnsByQuerySql(querySql);
    }

    @Override
    public List<ColumnInfo> getFullColumns(Integer datasourceId, String tableName) throws IOException {
        JobDatasource datasource = jobDatasourceService.getById(datasourceId);
        //queryTool组装
        if (ObjectUtil.isNull(datasource)) {
            return Lists.newArrayList();
        }
//        if (JdbcConstants.HBASE.equals(datasource.getDatasource())) {
//            return new HBaseQueryTool(datasource).getColumns(tableName);
//        } else if (JdbcConstants.MONGODB.equals(datasource.getDatasource())) {
//            return new MongoDBQueryTool(datasource).getColumns(tableName);
//        } else {
        BaseQueryTool queryTool = QueryToolFactory.getByDbType(datasource);
        return queryTool.getColumns(tableName);
//        }

    }

    @Override
    public List<JobDatasource> getDatasource() {
        List<JobDatasource> jobDatasources = this.jobDatasourceService.getBaseMapper().selectList(null);
        return jobDatasources;
    }

    @Override
    public List<String> getDistinctColumnValue(Integer datasourceId, String tableName, String columnName) {
        JobDatasource datasource = jobDatasourceService.getById(datasourceId);
        if (ObjectUtil.isNull(datasource)) {
            return Lists.newArrayList();
        }

        BaseQueryTool queryTool = QueryToolFactory.getByDbType(datasource);
        return queryTool.getDistinctColumnValue(datasourceId, tableName, columnName);
    }
}
